<?php
	require_once( 'core_DataBaseConnect.php' );
	require_once( '_configs.php' );
	
	class MemberBusinessDAO
	{
		
		// 멤버가 모델을 즐겨찾기에 등록했는지 여부
		public static function isBusinessFavorite( DataBaseConnect $con, 
													$login_member_id, 
													$businessCode )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_MEMBERBUSINESSFAVORITES." WHERE `B_CODE`='$businessCode' AND `OWNER_ID`='$login_member_id';";
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			return $fetch[ 'COUNT(SEQ)' ]=='1';
		}
		
		
		// 멤버가 모델을 즐겨찾기에 등록
		public static function insertBusinessFavorite( DataBaseConnect $con, 
														$login_member_id, 
														$businessCode )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_MEMBERBUSINESSFAVORITES." WHERE `B_CODE`='$businessCode' AND `OWNER_ID`='$login_member_id';";
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			if( (int)$fecth[ 'COUNT(SEQ)' ] >= 1 ) return;
			
			$query ='INSERT INTO '.configs::TABLE_MEMBERBUSINESSFAVORITES.
						"(
							`B_CODE`,
							`OWNER_ID`
						)
						VALUES
						(
							'$businessCode',
							'$login_member_id'
						);";
			return $con->query( $query );
		}
		
		// 멤버가 모델을 즐겨찾기에서 해제
		public static function deleteBusinessFavorite( DataBaseConnect $con, 
													$login_member_id, 
													$businessCode )
		{
			$query = 'DELETE FROM '.configs::TABLE_MEMBERBUSINESSFAVORITES." WHERE `B_CODE`='$businessCode' AND `OWNER_ID`='$login_member_id';";
			return $con->query( $query );
		}
		
		// 멤버가 모델의 댓글을 등록
		public static function insertBusinessComment( DataBaseConnect $con, 
													$login_member_id, 
													$businessCode,
													$comment )
		{
			$query = 'INSERT INTO '.configs::TABLE_MEMBERBUSINESSCOMMENTS.
						"(
							`B_CODE`,
							`OWNER_ID`,
							`COMMENT`,
							`REGISTER`
						)VALUES(
							'$businessCode',
							'$login_member_id',
							'$comment',
							NOW()
						);";
			return $con->query( $query );
		}
		
		// 모든 댓글 수 가져오기
		public static function countBusinessComments( DataBaseConnect $con,
													$businessCode )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_MEMBERBUSINESSCOMMENTS." WHERE `B_CODE`='$businessCode';";
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			return $fetch[ 'COUNT(SEQ)' ];
		}
		
		// 모든 댓글 목록 가져오기
		public static function selectBusinessComments( DataBaseConnect $con,
												$businessCode,
												$page,
												$numPerPage )
		{
			$startIndex = max( ($page-1)*$numPerPage, 0 );
			$query = 'SELECT t1.*, t2.NICKNAME 
							FROM '.configs::TABLE_MEMBERBUSINESSCOMMENTS.' AS t1 
							JOIN '.configs::TABLE_MEMBER." AS t2 
							ON t1.OWNER_ID = t2.SEQ
							WHERE 
								`B_CODE`='$businessCode' 
							LIMIT 
								$startIndex,$numPerPage;";
								
			return $con->query( $query );
		}
		
		
		// 댓글 삭제하기
		public static function deleteBusinessComment( DataBaseConnect $con,
												$seq,
												$login_member_id)
		{
			$query = 'DELETE FROM '.configs::TABLE_MEMBERBUSINESSCOMMENTS." WHERE `SEQ`='$seq' AND `OWNER_ID`='$login_member_id' LIMIT 1;;";
			$con->query( $query );
			return ( $con->affected_rows() > 0 );
		}
		
		// 특정 코멘트를 조회함
		public static function selectBusinessCommentBySeq( DataBaseConnect $con, $seq )
		{
			$query = 'SELECT * FROM '.configs::TABLE_MEMBERBUSINESSCOMMENTS." WHERE `SEQ`='$seq' LIMIT 1;";
			return $con->query( $query );
		}
		
		// 즐겨찾기 등록된 목록 수
		public static function countFavoriteBusinesses( DataBaseConnect $con, $login_member_id )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_MEMBERBUSINESSFAVORITES."
						WHERE
							`OWNER_ID`='$login_member_id'
						LIMIT 1;";
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			return $fetch[ 'COUNT(SEQ)' ];
		}
		
		// 즐겨찾기 등록된 목록 조회
		public static function selectFavoriteBusinesses( DataBaseConnect $con, $login_member_id, $page, $numPerPage )
		{
			$startIndex = max( ($page-1)*$numPerPage, 0 );
			
			$query = 'SELECT T2.* 
						FROM '.configs::TABLE_MEMBERBUSINESSFAVORITES.' AS T1 JOIN '.configs::TABLE_BUSINESS." AS T2
						ON 
							T1.B_CODE = T2.CODE
						WHERE
							T1.OWNER_ID = '$login_member_id'
						LIMIT
							$startIndex,$numPerPage;";
			return $con->query( $query );
		}
		
		// 댓글 남긴 목록 수
		public static function countCommentBusinesses( DataBaseConnect $con, $login_member_id )
		{
			$query = 'SELECT COUNT(Distinct `B_CODE`) FROM '.configs::TABLE_MEMBERBUSINESSCOMMENTS."
						WHERE
							`OWNER_ID`='$login_member_id'
						LIMIT 1;";
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			return $fetch[ 'COUNT(Distinct `B_CODE`)' ];
		}
		// 즐겨찾기 등록된 목록 조회
		public static function selectCommentBusinesses( DataBaseConnect $con, $login_member_id, $page, $numPerPage )
		{
			$startIndex = max( ($page-1)*$numPerPage, 0 );
			
			$query = 'SELECT Distinct T1.B_CODE,T2.* FROM '.configs::TABLE_MEMBERBUSINESSCOMMENTS.' AS T1 JOIN '.configs::TABLE_BUSINESS." AS T2
						ON 
							T1.B_CODE = T2.CODE
						WHERE
							T1.OWNER_ID = '$login_member_id'
						LIMIT
							$startIndex,$numPerPage;";
			return $con->query( $query );
		}
		
		// 법안 투표
		public static function insertBusinessVote( DataBaseConnect $con, 
														$login_member_id, 
														$businessCode,
														$voteCode )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_MEMBERBUSINESSVOTES."
						 WHERE 
						 	`B_CODE`='$businessCode' 
						 	AND 
							`OWNER_ID`='$login_member_id';";
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			if( (int)$fetch[ 'COUNT(SEQ)' ] >= 1 )
			{
				// 업데이트
				$query ='UPDATE '.configs::TABLE_MEMBERBUSINESSVOTES."
							SET
								`V_CODE`='$voteCode'
							WHERE 
								`B_CODE`='$businessCode' 
								AND 
								`OWNER_ID`='$login_member_id';";
			}else{
				// 입력
				$query ='INSERT INTO '.configs::TABLE_MEMBERBUSINESSVOTES.
							"(
								`B_CODE`,
								`OWNER_ID`,
								`V_CODE`
							)
							VALUES
							(
								'$businessCode',
								'$login_member_id',
								'$voteCode'
							);";
			}
			$con->query( $query );
		}
		
		// 멤버가 사업에 투표한 코드
		public static function selectBusinessVote( DataBaseConnect $con, 
													$login_member_id, 
													$businessCode )
		{
			$query = 'SELECT `V_CODE` FROM '.configs::TABLE_MEMBERBUSINESSVOTES."
						 WHERE 
						 	`B_CODE`='$businessCode' 
						 	AND 
							`OWNER_ID`='$login_member_id';";
			return $con->query( $query );
		}
	}
?>